# -*- coding: utf-8 -*-

import urllib2
import json
import hashlib
import random
import string
import time

headers = {"User-Agent":"Buyer/5.6.0 (com.ymatou.Shopping; build:5.6.0.1; iOS 10.0.0) Alamofire/5.6.0",
           "sign":"080f37571f6c65d9b8f0d881f120fadb1234ddgsfgda",
           "timestamp":int(round(time.time() * 1000))}

livingurl = 'https://app.ymatou.com/api/live/video/livinglist?pageindex=1&pagesize=200&tabid=0'
backurl = 'https://app.ymatou.com/api/live/video/backlist?pageindex=1&tabid=0'

request = urllib2.Request(livingurl, headers = headers)
response = urllib2.urlopen(request).read()

backlist_request = urllib2.Request(backurl, headers = headers)
backlist_resposne = urllib2.urlopen(backlist_request).read()
livelist = []

hjson = json.loads(response)
backlist_hjson = json.loads(backlist_resposne)
backlist = []

if len(hjson["list"]) == 0:
    print('没有正在进行的互动直播')
    exit();

if len(backlist_hjson["list"]) == 0:
    print('没有正在回放的直播')
    exit();

for i in range(len(hjson["list"])):

    if hjson["list"][i]["type"] != 1:
        continue;

    liveId = hjson["list"][i]["live"]["liveId"]
    sellerId = hjson["list"][i]["live"]["sellerInfo"]["id"]
    print(liveId)
    print(sellerId)

    if i == len(hjson["list"]) - 1:
         livelist.append(str(sellerId) + ',' + str(liveId));
    else:
        livelist.append(str(sellerId) + ',' + str(liveId) + '\n');

for i in range(len(backlist_hjson["list"])):

    liveId = backlist_hjson["list"][i]["liveId"]

    if i == len(backlist_hjson["list"]) - 1:
         backlist.append(str(liveId));
    else:
        backlist.append(str(liveId) + '\n');


with open('../videoliveid.dat', 'w') as f:
    f.write('sellerid,videoliveid\n')
    f.writelines(livelist)

with open('../historyliveid.dat', 'w') as f:
    f.write('historyliveid\n')
    f.writelines(backlist)
